package Server.security;


import Server.dao.DataResourcesDao;
import Server.entity.DataResources;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AutoShiro extends ShiroFilterFactoryBean {

    /**权限service*/

    /**
     * 初始化设置过滤链
     */
    public AutoShiro(DataResourcesDao fspDataRecordDao) {

        //FspUser fspUser = fspUserDao.isSelect("F_IPONE","15771929323");
        // 过滤器链11
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/login", "authc");
        filterChainDefinitionMap.put("/img/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/loginout", "loginout");



        //vsp授权
        List<DataResources> vspList = fspDataRecordDao.findByType(DataResources.Type.VIEW);
        for (DataResources vp : vspList) {
            if (vp.getStatus() == DataResources.Status.YES) {
                filterChainDefinitionMap.put("/vsp/" + vp.getUuid(), vp.getPermissions());
            }else{
                filterChainDefinitionMap.put("/vsp/" + vp.getUuid(), "authc,kickout,perms[band]");
            }
        }
        filterChainDefinitionMap.put("/home", "authc,kickout,perms[index]");

        //dsp授权
        List<DataResources> dspList = fspDataRecordDao.findByType(DataResources.Type.API);
        for (DataResources vp : dspList) {
            if (vp.getStatus() == DataResources.Status.YES) {
                filterChainDefinitionMap.put(vp.getUri(), vp.getPermissions());
            }else{
                filterChainDefinitionMap.put(vp.getUri(), "authc,kickout,perms[band]");
            }
        }

        //bsp授权

        List<DataResources> bspList = fspDataRecordDao.findByType(DataResources.Type.TABLE);
        for (DataResources vp : bspList) {
            if (vp.getStatus() == DataResources.Status.YES) {
                filterChainDefinitionMap.put("/bsp/" + vp.getUuid(), vp.getPermissions());
            }else{
                filterChainDefinitionMap.put("/bsp/" + vp.getUuid(), "authc,kickout,perms[band]");
            }
        }
        super.setFilterChainDefinitionMap(filterChainDefinitionMap);
    }
}
